<?php
function ShowHighlight($target, $mother){
	global $suri;
	if($suri['directive']=="/search" && $suri['value']!=""){
		$target = bindSearchHighlight($suri['value'], $target);
	}
	return $target;
}

function bindSearchHighlight($keyword, $content) {
    $src = array("/", "|");
    $dst = array("\/", "\|");
    if (!trim($keyword)) return $content;
	$tempContent = str_replace($src, $dst, quotemeta($keyword));
	$tempKeyword = $tempContent . "(?![^<]*>)";
    return preg_replace("/($tempKeyword)/i", "<span class='highlight'>\\1</span>", $content);
}

function highlightCss($target){
	global $configVal;
	requireComponent('Textcube.Function.misc');
	$data = misc::fetchConfigVal($configVal);
	if(is_null($data)){
		$data['textcolor']	= "#FFFFFF"; 
		$data['backcolor']	= "#6C92CA";
		$data['bold']		= 0; 
		$data['underline']	= 0;
		$data['italic']		= 0; 
	}
	$textcolor	= (strpos($data['textcolor'], "#")===0)?$data['textcolor']:"#".$data['textcolor'];
	$backcolor	= (strpos($data['backcolor'], "#")===0)?$data['backcolor']:"#".$data['backcolor'];
	if($textcolor)			$highLightCSS  = "color:{$textcolor};";
	if($backcolor)			$highLightCSS .= "background-color:{$backcolor};";
	if($data['bold'])		$highLightCSS .= "font-weight:bold;";
	if($data['underline'])	$highLightCSS .= "text-decoration:underline;";
	if($data['italic'])		$highLightCSS .= "font-style:italic";
	ob_start();
?>
<style type="text/css"> .highlight {<?php echo $highLightCSS;?>} </style>
<?php
	$target .= ob_get_contents();
	ob_end_clean();
	return $target;
}

function SearchHighlightDataSet($DATA){
	requireComponent('Textcube.Function.misc');
	$cfg = misc::fetchConfigVal($DATA);
	return true;
}
?>